package cemp.service;

import cemp.common.dto.StockCurrentSendDto;
import cemp.conf.StockMailSender;
import cemp.config.InfluxDBUtils;
import cemp.domain.response.*;
import cemp.entity.*;
import cemp.mapper.*;
import cemp.redis.util.RedisUtils;
import cemp.util.*;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.api.AlarmApi;
import com.api.StockCommonQueryApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.cache.LoadingCache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static cemp.common.constant.StockCommonConstant.*;
import static cemp.redis.constant.RedisKey.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class KpiDataService {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    InfluxDB influxDB;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    InfluxDBUtils influxDBUtils;
    @Autowired
    StockMailSender stockMailSender;
    @Autowired
    StockSendMailService stockSendMailService;
    @Autowired
    StockAllService stockAllService;
    @Autowired
    StockDailyStatusService stockDailyStatusService;
    @Autowired
    StockPlateService stockPlateService;
    @Autowired
    StockPlateStocksService stockPlateStocksService;
    @Autowired
    StockKpiDayService stockKpiDayService;
    @Autowired
    AlarmApi alarmApi;
    @Autowired
    StockCommonQueryApi stockCommonQueryApi;
    @Autowired
    LoadingCache guavaHoliday;
    @Autowired
    ThreadPoolTaskExecutor baseStockExecutor;
    @Autowired
    ThreadPoolTaskExecutor plateStocksExecutor;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    KafkaTemplate<byte[], byte[]> kafkaTemplate;
    @Autowired
    MongoTemplate mongoTemplate;

    public void kpiTDI(String staDate){

        List<Map> trueList = new ArrayList<>();
        List<Map> falseList = new ArrayList<>();

        String beginDate = DateUtil.getBeginDate(staDate,3,stockCommonQueryApi);
        String endDate = DateUtil.localDate2Str(DateUtil.str2LocalDate(staDate).plusDays(1)) ;
        //所有股票 近3个交易日的成交数据
        List<StockKpiDay> kpiDays = stockKpiDayService.getRecent(beginDate,endDate);
        Map<String,List<StockKpiDay>> kpiMap = kpiDays.stream().collect(Collectors.groupingBy(StockKpiDay::getStockCode));
        kpiMap.forEach(new BiConsumer<String, List<StockKpiDay>>() {
            @Override
            public void accept(String stockCode, List<StockKpiDay> stockKpiDays) {
                boolean isTDI = false;
                Map data =new HashMap();
                data.put("stockCode",stockCode);
                data.put("staDate",staDate);

                if(stockKpiDays != null && stockKpiDays.size() == 3){
                    List<StockKpiDay> filterList = stockKpiDays.stream().filter(stockKpiDay -> stockKpiDay.getPriceIncrease().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
                    if(filterList.size() ==3){
                        isTDI = true;
                    }
                }
                if(isTDI){
                    data.put("TDI",true);
                    trueList.add(data);
                }else{
                    data.put("TDI",false);
                    falseList.add(data);
                }
            }
        });
        mongoTemplate.insert(trueList,"stock_kpi");
        mongoTemplate.insert(falseList,"stock_kpi");



    }


}
